<template>
  <van-button
      round
      size="small"
      v-if="article.isFollowed"
      @click="onFollow"
      :loading="loading"
    >已关注</van-button>
    <van-button
      type="info"
      color="#3296fa"
      round
      size="small"
      icon="plus"
      v-else
      @click="onFollow"
      :loading="loading"
    >关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  model: {
    prop: 'isFollowed',
    event: 'update-is_followed'
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: false
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onFollow () {
      this.loading = true
      try {
        if (this.isFollowed) {
          // 已关注, 取消关注
          await deleteFollow(this.userId)
          // this.article.value = false
        } else {
          // 没有关注,添加关注
          addFollow(this.userId)
          // this.article.value = true
        }
        // this.article.value = !this.article.value
        this.$emit('update-is_followed', !this.isFollowed)
      } catch (error) {
        let message = '操作失败,请重试!'
        if (error.response && error.response.status === 400) {
          message = '不能自己关注自己'
        }
        this.$toast(message)
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">

</style>
